﻿@{
    Layout = null;
}

@using DDit.Core.Data.Entity.SystemEntity;

@Styles.Render("~/Scripts", "~/Content")
@Scripts.Render("~/bundles/js")

<script type="text/javascript">
    $(function () {
        $("#dicTree").fancytree({
            selectMode: 2,
            source: {
                url: "/Dictionary/GetGetDicategoryList",
                cache: false
            },
            activate: function (event, data) {
                var obj = data.node;
                var dicID = obj.data.dicid;
                $("#dicID").val(dicID);
                table.ajax.reload();
            }
        })
        
        var table = $('#dicTable').custromTable({
            ajax: {
                url: "/Dictionary/GetDicValueList",
                data: function (d) {
                    d.DicCategoryID = $("#dicID").val();
                    return JSON.stringify(d);
                }
            },
            columns: [
                { data: 'ID', visible: false },
                { data: 'DicCategory.Category', name: "DicCategoryID", title: "字典分类" },
                { data: 'DicValue', title: "字典值" },
                {
                    data: 'Enabled', title: "是否可用", render: function (data, type, row, meta) {
                         return "<img src='../Images/"+data+".png' >"
                    }
                },
                { data: 'CreateTime', title: "创建时间", width: 100 }
            ]
        });

        //绑定Ajax的内容
        var getDicategory= function() {
            $.post("/Dictionary/GetGetDicategoryList", {}, function (data) {
                odata = data;
            });
        }

        getDicategory();

        //字典层
        var openDicValueLayer = function () {
            mainLayer = parent.layer.open({
                type: 1,
                title: "新增字典",
                area: ['500px', '280px'],
                closeBtn: 1,
                shadeClose: false,
                skin: 'layerStyle',
                content: $("#dicValueLayer").html(),
                btn:[dditConfig.btn.close, dditConfig.btn.comfrie],
                yes: function (index, layero) {
                    comfrieDicValueBtn(index);
                },
                success: function (i, l) {
                    parent.$("#dicValueForm").FromEvent(function () {

                        $("#selDicCategory").change(function () {
                           if($(this).val()!=null)
                            $(this).valid();
                        });

                        //验证表单
                        $("#dicValueForm").validate({
                            ignore: "",
                            rules: {
                                DicCategoryID:{
                                    required: true
                                },
                                DicValue: {
                                    required: true
                                }
                            },
                            messages: {
                                DicCategoryID: {
                                    required: "必填"
                                },
                                DicValue: {
                                    required: "必填"
                                }
                            },
                            highlight: function (element) {
                                var type = element.tagName;
                                if (type == "SELECT") {
                                    $(element).next("span")
                                        .children()
                                        .children()
                                        .css({ "border-color": "#a94442" })
                                }
                                       
                                $(element).closest('.form-group').addClass('has-error');
                            },

                            success: function (label) {
                                var element = label.prev();
                                var type = element[0].tagName;
                                if (type == "SPAN") {
                                    element.children().children().css({ "border-color": "" })
                                }
                                label.closest('.form-group').removeClass('has-error');
                                label.remove();
                            }
                        })

                        var control = $('#selDicCategory');                
                        //设置Select2的处理
                        control.select2({
                            minimumResultsForSearch: -1
                        });

                        var currentW=document.getElementById("DictionaryIndex").contentWindow;
                        var odata = currentW.odata;
                        var val = currentW.$("#dicTree").fancytree("getTree");
                        var node= val.getActiveNode()
                        $.each(odata, function (i, item) {
                            control.append("<option value='" + item.dicid + "'>&nbsp;" + item.title + "</option>");
                        });
                        
                        control.val(node != null?node.data.dicid:null).trigger("change");
                        
                    });
                }
            });
        }

        var comfrieDicValueBtn = function () {
            var dicValueform = parent.$("#dicValueForm");
            if (dicValueform.valid()) {
                dicValueform.ajaxSubmit({
                    success: function (data) {
                        if (data.result) {
                            parent.layer.close(parent.layer.index);
                            table.ajax.reload();          //刷新列表
                            alertify.success("操作成功！");    //弹出消息日志
                        } else {
                            alertify.error(data.message);
                        }
                    }
                });
            }
        };

        //字典分类层
        var openDicategoryLayer = function () {
            mainLayer = parent.layer.open({
                type: 1,
                title: "新增字典分类",
                area: ['500px', '200px'],
                closeBtn: 1,
                shadeClose: false,
                skin: 'layerStyle',
                content: $("#dicategoryLayer").html(),
                btn:[dditConfig.btn.close, dditConfig.btn.comfrie],
                yes: function (index, layero) {
                    comfrieDicategoryBtn(index);
                },
                success: function (i, l) {
                    parent.$("#dicategoryForm").FromEvent(function () {
                        //验证表单
                        $("#dicategoryForm").validate({
                            rules: {
                                Category: {
                                    required: true
                                }
                            },
                            messages: {
                                Category: {
                                    required: "必填"
                                }
                            },
                            highlight: function (element) {
                                $(element).closest('.form-group').addClass('has-error');
                            },

                            success: function (label) {
                                label.closest('.form-group').removeClass('has-error');
                                label.remove();
                            }
                        })

                    });
                }
            });
        }

        var comfrieDicategoryBtn = function ()  {
            var dicform = parent.$("#dicategoryForm");
            if (dicform.valid()) {
                dicform.ajaxSubmit({
                    success: function (data) {
                        if (data.result) {
                            parent.layer.close(parent.layer.index);
                            table.ajax.reload();       
                            var tree =$("#dicTree").fancytree("getTree")
                            tree.reload();
                            getDicategory();
                            alertify.success("操作成功！");    
                        } else {
                            alertify.error(data.message);
                        }
                    }
                });
            }
        };

        //新增字典
        $("#dicValueCreate").click(function () {
            openDicValueLayer();
        });

        //编辑字典
        $("#dicValueEdit").click(function () {
            var cc = table.rows('.selected')
            if (cc[0].length > 0) {
                var model = cc.data();
                openDicValueLayer();
                parent.layer.title("编辑字典", parent.layer.index);
                parent.$("#dicValueForm").setForm(model[0]);
                parent.$("#dicValueEnabled").attr("checked", model[0].Enabled);

            } else {
                alertify.log("请选择一条操作信息！");
            }
        });

        //新增字典分类
        $("#dicategoryCreate").click(function () {
            openDicategoryLayer();
        });

        //编辑字典分类
        $("#dicategoryEdit").click(function () {
            var tree = $("#dicTree").fancytree("getTree")
            var currentNote = tree.getActiveNode();
            if (currentNote == null) {
                alertify.success("请选择一条要编辑的字典分类！");
                return;
            }
            openDicategoryLayer();
            parent.layer.title("编辑字典分类", parent.layer.index);
            parent.$("#dicategoryForm").setForm({
                Category: currentNote.title,
                ID: currentNote.data.dicid
            })
        });
   
    });
</script>

<div class="container-fluid" style="height:100%">
    <div class="row" style="height:100%">
        <div class="col-md-2 diclayer" >
            <div class="dictitle">字典分类 </div>
            <div id="dicTree"></div>
        </div>
        <div class="col-md-10">
            <div class="panel panel-default" style="margin-top: 10px">
                <ul class="list-group" >
                    <li class="list-group-item">
                        <button type="button" class="btn btn-default" id="dicValueCreate">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 新增字典
                        </button>
                        <button type="button" class="btn btn-default" id="dicValueEdit">
                            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 编辑字典
                        </button>
                        <button type="button" class="btn btn-default" id="dicategoryCreate">
                            <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> 新增分类
                        </button>
                        <button type='button' class='btn btn-default' id="dicategoryEdit">
                            <span class='glyphicon glyphicon-edit' aria-hidden='true'></span> 编辑分类
                        </button>
                       
                    </li>
                </ul>
            </div>
            <input type="hidden" id="dicID" value="0" />
            <table id="dicTable" class="table table-bordered table-striped w100f" cellspacing="0"></table>
        </div>
    </div>
</div>

<div class="displayn" id="dicategoryLayer">
    <form class="form-horizontal formPadding" id="dicategoryForm" method="post" action="/Dictionary/OperationDicategory">
        <div class="form-group">
            @Html.Hidden("ID")
            <label for="Category" class="col-sm-2 control-label labsty">分类名称<span style="color:red">*</span></label>
            <div class="col-sm-10 pad-r20">
                <input type="text" class="form-control" maxlength="15" name="Category" placeholder="名称">
            </div>
        </div>
    </form>
</div>

<div class="displayn" id="dicValueLayer">
    <form class="form-horizontal formPadding" id="dicValueForm" method="post" action="/Dictionary/OperationDicValue">
        @Html.Hidden("ID")
        <div class="form-group">
            <label for="CategoryName" class="col-sm-2 control-label labsty">字典分类<span style="color:red">*</span></label>
            <div class="col-sm-10 pad-r20">
                <select class="selDicCategory" name="DicCategoryID" id="selDicCategory" style="width:100%"></select>
            </div>
        </div>
        <div class="form-group">
            <label for="DicValue" class="col-sm-2 control-label labsty">字典名称<span style="color:red">*</span></label>
            <div class="col-sm-10 pad-r20">
                <input type="text" class="form-control" maxlength="15" name="DicValue" placeholder="名称">
            </div>
        </div>
        <div class="form-group">
            <label for="Category" class="col-sm-2 control-label labsty"></label>
            <div class="col-sm-10 pad-r20">
                <div class="checkbox checkbox-success">
                    <input type="checkbox" name="Enabled" checked="checked" id="dicValueEnabled" />
                    <label>是否可用</label>
                </div>
            </div>
        </div>
    </form>
</div>